﻿using MqttLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace remote_config_tools
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 引用mqtt库
        /// </summary>
        public mqttlib mtlib;
        public control_classs ctrlobj;
        public fileOp objFileOp;
        public bool toggle_bit;
        public string file_name;
        public string input_mac;
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 初始化变量和对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            mtlib = new mqttlib(this);
            ctrlobj = new control_classs(this);
            objFileOp = new fileOp(this);
            toggle_bit = false;
            button1.Enabled = false;
            button3.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;
            textBox2.ReadOnly = true;
            file_name = "";
            comboBox1.SelectedIndex = comboBox1.Items.Count > 0 ? 0 : -1;
        }
        public StatusStrip getSS()
        {
            return this.statusStrip1;
        }
        public ToolStripStatusLabel getTSS()
        {
            return this.toolStripStatusLabel1;

        }
        public Label getParaLabel()
        {
            return this.label12;

        }
        public Label getUpgradeLabel()
        {
            return this.label13;

        }
        public Label getUpgradeStateLabel()
        {
            return this.label11;

        }
        public Label getUpgradePackNumLabel()
        {
            return this.label14;

        }
        public ProgressBar getParaprogress()
        {
            return this.progressBar1;

        }
        public Button getButton()
        {
            return this.button3;

        }
        public TextBox getTextboxA()
        {
            return this.textBox3;
        }
        public TextBox getTextboxB()
        {
            return this.textBox4;
        }
        public TextBox getTextboxC()
        {
            return this.textBox5;
        }
        public TextBox getTextboxD()
        {
            return this.textBox6;
        }
        public TextBox getTextboxE()
        {
            return this.textBox7;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "二进制文件(*.bin)|*.bin";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = openFileDialog1.FileName;
                file_name= openFileDialog1.FileName ;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (file_name == String.Empty) return;
            Thread t = new Thread(new ParameterizedThreadStart(objFileOp.fileDownload));
            t.Start(file_name);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (String.Empty == textBox1.Text.Trim()) return;

            mtlib.mqttConnectToServer("iotplc.cn","1883");
            mtlib.mqttSubscribe("/fw/alphair2/"+textBox1.Text.Trim()+"/up");
            input_mac = textBox1.Text.Trim();
            toggle_bit = true;
            ctrlobj.UpStatusToolCtrlText(this.statusStrip1, this.toolStripStatusLabel1, "连接操作", "连接成功");
            button2.Enabled = false;
            button1.Enabled = true;
            textBox1.Enabled = false;
            button3.Enabled = true;
            button5.Enabled = true;
            button6.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            mtlib.mqttDisonnectToServer();
            ctrlobj.UpStatusToolCtrlText(this.statusStrip1, this.toolStripStatusLabel1, "断开操作", "断开成功");
            toggle_bit = false;
            button2.Enabled = true;
            button1.Enabled = false;
            button3.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;
            textBox1.Enabled = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.ctrlobj.UpLabelText(this.getParaLabel(), "");

            if (textBox3.Text.Trim()==String.Empty|| textBox4.Text.Trim() == String.Empty 
                || textBox5.Text.Trim() == String.Empty || textBox6.Text.Trim() == String.Empty
                || textBox7.Text.Trim() == String.Empty)
            {
                return ;
            }

            try
            {
                int sensor_t = comboBox1.SelectedIndex+1;

                double av = Convert.ToDouble(textBox3.Text) * 100;
                double bv = Convert.ToDouble(textBox4.Text) * 100;
                double cv = Convert.ToDouble(textBox5.Text) * 100;
                double dv = Convert.ToDouble(textBox6.Text) * 100;
                double ev = Convert.ToDouble(textBox7.Text) * 100;

                byte[] para = new byte[10];

                para[0] = (byte)((UInt16)av >> 8);
                para[1] = (byte)((UInt16)av >> 0);
                para[2] = (byte)((UInt16)bv >> 8);
                para[3] = (byte)((UInt16)bv >> 0);
                para[4] = (byte)((UInt16)cv >> 8);
                para[5] = (byte)((UInt16)cv >> 0);
                para[6] = (byte)((UInt16)dv >> 8);
                para[7] = (byte)((UInt16)dv >> 0);
                para[8] = (byte)((UInt16)ev >> 8);
                para[9] = (byte)((UInt16)ev >> 0);

                mtlib.mqttPublishSomething("/fw/alphair2/" + input_mac + "/down", dt_protocol.wrapper_para_data(sensor_t,para), QoS.BestEfforts, false);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                this.ctrlobj.UpLabelText(this.getParaLabel(), "");

                int sensor_t = comboBox1.SelectedIndex+1;

                byte[] para = new byte[2];

                para[0] = (byte)(sensor_t >> 8);
                para[1] = (byte)(sensor_t >> 0);

                mtlib.mqttPublishSomething("/fw/alphair2/" + input_mac + "/down", dt_protocol.wrapper_para_qurey(0x0000, para), QoS.BestEfforts, false);
            }
            catch (Exception ex)
            {

            }
        }

        private void button6_MouseHover(object sender, EventArgs e)
        {
            toolTip1.Show("请选择对应查询的传感器类型", this.button6);
        }

        private void button5_MouseHover(object sender, EventArgs e)
        {
            toolTip1.Show("请选择对应查询的传感器类型，并填写相应系数", this.button5);
        }

        private void button3_MouseHover(object sender, EventArgs e)
        {
            toolTip1.Show("请先加载文件，然后升级", this.button3);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                mtlib.mqttDisonnectToServer();
            }
            catch (Exception ex)
            {
            }
        }
    }
}
